package org.feng.service.impl;

import org.feng.entity.Employee;
import org.feng.service.EmployeeService;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

/**
 * 员工业务接口功能实现
 *
 * @version v1.0
 * @author: fengjinsong
 * @date: 2022年06月07日 21时58分
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    /**
     * 模拟员工列表数据：使用 @PostConstruct初始化数据
     */
    private static Set<Employee> employeeSet;

    /**
     * 通过员工ID查找某个员工的信息
     *
     * @param id 员工id
     * @return 指定的员工信息
     */
    @Override
    public Mono<Employee> getEmpById(String id) {
        Optional<Employee> empOptional = employeeSet.stream()
                .filter(employee -> Objects.equals(id, employee.getId()))
                .distinct().findFirst();
        return Mono.justOrEmpty(empOptional);
    }

    @PostConstruct
    private void initEmployeeSet() {
        employeeSet = new HashSet<>();

        employeeSet.add(new Employee("1221", "HC20210321", "刘玉婷", 34, LocalDate.of(1992, 3, 21), LocalDate.of(2021, 4, 22)));
        employeeSet.add(new Employee("2333", "HC20210381", "王嘎嘎", 23, LocalDate.of(1999, 7, 11), LocalDate.of(2022, 3, 29)));
    }
}
